package com.litchi.aopdemo.config;

import com.litchi.aopdemo.aspect.MyAopAspect;
import com.litchi.aopdemo.dao.UserDao;
import com.litchi.aopdemo.dao.UserDaoImpl;
import org.springframework.aop.framework.ProxyFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author LitchiShell
 * @description: AOP配置类
 * @create 2022-04-16
 */
@Configuration
public class AopBeanConfig {

    // 目标类
    @Bean
    public UserDao userDao() {
        return new UserDaoImpl();
    }

    // 切面类
    @Bean
    public MyAopAspect myAopAspect() {
        return new MyAopAspect();
    }

    // 使用Spring代理工厂定义一个名称为userDaoProxy的代理对象
    @Bean
    public ProxyFactoryBean userDaoProxy() throws ClassNotFoundException {
        ProxyFactoryBean proxyFactoryBean = new ProxyFactoryBean();
        // 指定代理实现的接口
        proxyFactoryBean.setProxyInterfaces(userDao().getClass().getInterfaces());
        // 指定目标对象
        proxyFactoryBean.setTargetName("userDao");
        // 指定切面，织入环绕通知
        proxyFactoryBean.setInterceptorNames("myAopAspect");
        // 指定代理方式，true:使用CGLIB，false(默认)：使用jdk动态代理
        proxyFactoryBean.setProxyTargetClass(true);
        return proxyFactoryBean;
    }

}
